<?php
class Obj_System_VirtualControllerPlugin extends Zend_Controller_Plugin_Abstract
{
	
	public function routeShutdown(Zend_Controller_Request_Abstract &$request)
    {
    	$front = Zend_Controller_Front::getInstance();
    	if (!$front->getDispatcher()->isDispatchable($request)) {
    		$path = rtrim(dirname($_SERVER['SCRIPT_NAME']), '/');
    		$str = $path != '' ? str_replace($path.'/', '', $_SERVER['REQUEST_URI']) : trim($_SERVER['REQUEST_URI'], '/');
    		$ar = explode('/', $str);
    		$arParams = array();
    		
    		foreach ($ar as $v) {
    			if (!in_array($v, array('page', 'index')) && intval($v) == 0) $arParams[] = $v;
    		}
    		$request->setControllerName('search');
    		$request->setModuleName('default');
    		$request->setActionName('index');
    		Zend_Registry::set('parsedParams', $arParams);
    		
    		for ($i = 0; $i < count($ar); $i++) {
    			if ($ar[$i] == 'page' && isset($ar[$i + 1]) && $ar[$i + 1] > 0) {
    				Zend_Registry::set('requestPage', $ar[$i + 1]);
    				break;
    			}
    		}
    	}
    	else {
    		if ($request->getModuleName() == 'default') {
	    		if ($request->getControllerName() == 'news') {
	    			if ($request->getActionName() != 'index') {
	    				$id = $request->getActionName();
	    				$request->setActionName('index');
	    				$request->setParam('id', $id);
	    			}
	    		}
	    		if ($request->getControllerName() == 'articles') {
	    			if ($request->getActionName() != 'index') {
	    				$id = $request->getActionName();
	    				$request->setActionName('index');
	    				$request->setParam('id', $id);
	    			}
	    		}
    		}
    	}
    }

}

